package com.tian.scroll.nested_scroll.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.core.view.NestedScrollingParent;
import androidx.core.view.NestedScrollingParentHelper;

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2021/1/20 14:43
 */
public class NestedScrollParentLayout extends LinearLayout implements NestedScrollingParent {

    public static final String TAG = "NestedScrollParent";

    private NestedScrollingParentHelper nestedScrollingParentHelper;

    public NestedScrollParentLayout(Context context) {
        super(context);
    }

    public NestedScrollParentLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NestedScrollParentLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public NestedScrollingParentHelper getNestedScrollingParentHelper() {
        if (nestedScrollingParentHelper == null) {
            nestedScrollingParentHelper = new NestedScrollingParentHelper(this);
        }
        return nestedScrollingParentHelper;
    }


    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        if (child instanceof NestedScrollChildLayout) {
            return true;
        }
        return true;
    }


    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        getNestedScrollingParentHelper().onNestedScrollAccepted(child, target, axes);
    }


    @Override
    public void onStopNestedScroll(View child) {
        getNestedScrollingParentHelper().onStopNestedScroll(child);
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        Log.i(TAG, "onNestedScroll: dyConsumed=" + dxConsumed + "  dyUnconsumed=" + dyUnconsumed);
        //父控件后滑动为显示上部控件
        if (getScrollY() > 0 && dyUnconsumed < 0) {
            if (getScrollY() + dyUnconsumed >0) {
                scrollBy(0, dyUnconsumed);
            } else {
                scrollTo(0, 0);
            }
        }
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(target, dx, dy, consumed);
        Log.i(TAG, "onNestedPreScroll: dx=" + dx + " dy=" + dy);
        View view = getChildAt(0);
        //父控件先滑动为隐藏上部控件，
        if (getScrollY() < view.getHeight() && dy > 0) {
            if (getScrollY() + dy <= view.getHeight()) {
                scrollBy(0, dy);
                consumed[1] = dy;
            } else {
                consumed[1] = view.getHeight() - getScrollY();
                scrollTo(0, view.getHeight());
            }
        }
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        return super.onNestedFling(target, velocityX, velocityY, consumed);
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        return super.onNestedPreFling(target, velocityX, velocityY);
    }
}
